
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
var action = "/report/stat/country.do";

$(document).ready(function () {

	$("#year").combobox("setValue", 2015);
	var year = $("#year").combobox("getValue");
	
	var curPage = $('#page').val();
	var pageSize = $('#pageSize').val();
	
	query(curPage, pageSize);
});

var CRUD = {
	queryList: function () {
		
		var curPage = $('#page').val();
		var pageSize = $('#pageSize').val();
		
		query(curPage, pageSize);
		
		try{window.parent.mask();}catch(err){}
	}
};

function query(curPage, pageSize) {
	
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	
	$('#page').val(curPage);
	$('#pageSize').val(pageSize);

	var year = $("#year").combobox("getValue");

	var reportShowColumns = [
	 		defineTableReportAllColumns["iname"],
	 		defineTableReportAllColumns["code"],
	 		defineTableReportAllColumns["unit"],
	 		defineTableReportAllColumns["places"]
	 		];
	var c = {field: year, title: '数值', width: 50, align: 'right',formatter: function (value, row, index) {
		return commafy(value + "");
    }};
	reportShowColumns.push(c);
	reportShowColumns.push(defineTableReportAllColumns["sname"]);
	reportShowColumns.push(defineTableReportAllColumns["memo"]);

	$.get(action, {"year": year, "page": curPage, "pageSize": pageSize,"formsid":1}, function (json) {
		var data = json.data;
		var total = data["size"];
		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage-1) * pageSize;
		var dataLength = 0;

		data = data["results"];
		if(data!=null){
			dataLength = data.length;
		}
		$('#tt').datagrid( {
			title:'报表列表',
			width : '100%',
			height : 'auto',
			fitColumns: true,
			columns: [
				reportShowColumns
			],
			singleSelect:true,// 是否单选
			loadMsg: '数据加载中,请稍后......',
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [pageSize, 50, 100],
			rownumbers: true,
			pagination: true,//打开翻页功能
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [pageSize, 50, 100],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
				query(curPage, pageSize);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		try{window.parent.unmask();}catch(err){}
	}, "json");
}


//$(document).ready(function () {
//
//	var nowYear = formatDate(new Date(), "yyyy");
//	$("#year").combobox("setValue", nowYear);
//	
//	$('#ff').form( {
//		success : function(json) {
//
//			json = eval('(' + json + ')');
//			var data = json.data;
//			var total = data["size"];
//			data = data["results"];
//			
//			//画下方的表格图
//            $('#tt').datagrid( {
//                width : '100%',
//                height : 'auto',
//                fitColumns : true,
//                columns : defineTableReportColumns,
//                rownumbers: false,
//                singleSelect:true,
//                view: detailview,
//                detailFormatter: function (index, row) {
//                	
////                    var depart = "<div style='float:left; left:10px;'><a href='#' style='text-decoration: underline;color: #0000FF' onclick=\"showDepart('" + row.forms + "','" + row.year + "','" + row.indicator + "');\">" + "部门数据分析" + "</a>";
////                    var year = "&nbsp;&nbsp;<a href='#' style='text-decoration: underline;color: #0000FF' onclick=\"showYear('" + row.forms + "','" + row.departid + "','" + row.indicator + "');\">" + "历年数据分析" + "</a></div>";
////                    
////                    if(!isEmpty($("#level").val()) && $("#level").val()!=1){
////                        return depart + year;
////                    }else{
////                    	return "";
////                    }
//                	
//                	return "";
//                }
//            
//            });
//
//		    $('#tt').datagrid("loadData" ,data);
//			try{window.parent.unmask();}catch(err){}
//		}
//	});
//	
//    CRUD.queryList();
//    InitForm();
//});

//var CRUD = {
//    queryList: function () {
//       try{window.parent.mask();}catch(err){}
//       
//       if(!isEmpty($("#level").val()) && $("#level").val()!=1){
//           $("#departid").val(getDepartId());
//       }
//	   $('#ff').submit();
//    }
//};

function InitForm() {
    var width = 500;
    var height = window.screen.height;
    if (height > 500) {
        height = 400;
    }
    $('#dialogAdd').dialog({
        closed: true,
        title: "添加指标分类",
        width: width,
        height: height
    });
}

function setEmptyForm(value) {
	if(typeof(value) == "undefined"||value == "null"){
		return "";
	}else{
		return value;
	}
}

$(function(){
    var provinceDepartId = $('#provinceDepartId').combobox({
        url:'/department/samePid.do?pid=0',
        editable:false,
        valueField:'id',
        textField:'name',
        onSelect:function(departTemp){
            //刷新数据，重新读取省份下的城市，并清空当前输入的值
            cityId.combobox({
                disabled:false,
                url:'/department/samePid.do?pid='+departTemp.id,
                valueField:'id',
                textField:'name'
            }).combobox('clear');
        }
    });
    var cityId = $('#cityId').combobox({
        disabled:true,
        url:'/department/samePid.do?pid='+provinceDepartId.val(),
        valueField:'id',
        textField:'name',
        onSelect:function(departTemp){
            //刷新数据，重新读取省份下的城市，并清空当前输入的值
        	townId.combobox({
                disabled:false,
                url:'/department/samePid.do?pid='+departTemp.id,
                valueField:'id',
                textField:'name'
            }).combobox('clear');
        }
    });
    var townId = $('#townId').combobox({
        disabled:true,
        url:'/department/samePid.do?pid='+cityId.val(),
        valueField:'id',
        textField:'name'
    });
});

function getDepartId() {
	
	var provinceDepartId = $('#provinceDepartId').combobox("getValue");
    var cityId = $('#cityId').combobox("getValue");
    var townId = $('#townId').combobox("getValue");
    
    var departid=null;
    
    if(!isEmpty(townId)&&townId != 0){
 	   departid = townId;
    }else if(!isEmpty(cityId) && cityId != 0){
 	   departid = cityId;
    }else if(!isEmpty(provinceDepartId) && provinceDepartId != 0){
    	departid = provinceDepartId;
	}else{
		departid = 0;
	}
    return departid;
}


//创建同年份，同部门，历年列表页面
function showDepart(forms,year,indicator) {

	var page = $('#page').val();
	var pageSize = $('#pageSize').val();
	var provinceDepartId = $('#provinceDepartId').combobox("getValue");
    var cityId = $('#cityId').combobox("getValue");
    var townId = $('#townId').combobox("getValue");

    var src = "/report/view/view.do?show=depart"
    	+"&page="+page
    	+"&pageSize="+pageSize
    	+"&year="+year
    	+"&forms="+forms
    	+"&provinceDepartId="+provinceDepartId
    	+"&cityId="+cityId
    	+"&townId="+townId
    	;
    var content = '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="' + src + '"></iframe>';
    
    window.parent.addTab("部门指标分析", content, -1); 
}

//创建同部门,同指标,各部门列表页面
function showYear(forms,departid,indicator) {
	
	var page = $('#page').val();
	var pageSize = $('#pageSize').val();
	var provinceDepartId = $('#provinceDepartId').combobox("getValue");
	var cityId = $('#cityId').combobox("getValue");
	var townId = $('#townId').combobox("getValue");
	
	var src = "/report/view/view.do?show=year"
		+"&page="+page
		+"&pageSize="+pageSize
		+"&departid="+departid
		+"&forms="+forms
		+"&indicator="+indicator
		+"&provinceDepartId="+provinceDepartId
		+"&cityId="+cityId
		+"&townId="+townId
		;
	var content = '<iframe style="width:100%;height:100%;" scrolling="auto" frameborder="0" src="' + src + '"></iframe>';
	
	window.parent.addTab("历年数据分析", content, -1); 
}
	